package com.icee.myth.deamon.channelHandler;

import com.icee.myth.deamon.ServerMessageQueue;
import com.icee.myth.common.protobufMessage.ProtobufMessageType;
import com.icee.myth.deamon.channelHandler.builder.DeamonMessageBuilder;
import com.icee.myth.protobuf.DeamonToManagerProtocol;
import com.icee.myth.protobuf.DeamonToManagerProtocol.StartServer;
import com.icee.myth.utils.LogConsts;
import com.icee.myth.utils.MLogger;
import com.icee.myth.utils.StackTraceUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.IOException;

/**
 *
 * @author liuxianke
 */
public class DeamonToManagerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // Add cluster channel connected message to message queue
        ServerMessageQueue.queue().offer(DeamonMessageBuilder.buildManagerConnectMessage(ctx.channel()));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // Add cluster channel close message to message queue
        ServerMessageQueue.queue().offer(DeamonMessageBuilder.buildManagerCloseMessage(ctx.channel()));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            // add cluster channel received message to message queue
            ByteBuf cb = (ByteBuf) msg;
            short type = cb.readShort();
            int length = cb.readInt();

            switch (type) {
                case ProtobufMessageType.MANAGER2DEAMON_HEARTBEAT: {
                    ServerMessageQueue.queue().offer(DeamonMessageBuilder.buildManagerHeartbeatMessage());
                    break;
                }
                case ProtobufMessageType.MANAGER2DEAMON_STARTSERVER: {
                    StartServer startService = DeamonToManagerProtocol.StartServer.getDefaultInstance().newBuilderForType().mergeFrom(new ByteBufInputStream(cb)).build();
                    ServerMessageQueue.queue().offer(DeamonMessageBuilder.buildStartServerMessage(startService.getVmOptionStr(), startService.getParamStr()));
                    break;
                }
                case ProtobufMessageType.MANAGER2DEAMON_SERVERCONFIGDATA: {
                    DeamonToManagerProtocol.ServerConfigData serverConfigData = DeamonToManagerProtocol.ServerConfigData.getDefaultInstance().newBuilderForType().mergeFrom(new ByteBufInputStream(cb)).build();
                    ServerMessageQueue.queue().offer(DeamonMessageBuilder.buildServerConfigDataMessage(serverConfigData.getServerId(), serverConfigData.getConfigData()));
                    break;
                }
                // TODO: other message
            }
        } catch (IOException ex) {
            MLogger.getlogger().log(LogConsts.LOGTYPE_NETERR, StackTraceUtil.getStackTrace(ex));
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        MLogger.getlogger().debuglog(LogConsts.LOGLEVEL_DEBUG, StackTraceUtil.getStackTrace(cause));
//        MLogger.getlogger().log(LogConsts.LOGTYPE_NETERR, StackTraceUtil.getStackTrace(cause.getCause()));
        // TODO:关闭与Cluster的连接
//        ctx.channel().close();
    }

}
